
#include "AboutBox.h"
#include <wx/mstream.h>
#include <wx/gbsizer.h>
#include <wx/button.h>

IMPLEMENT_DYNAMIC_CLASS( AboutBox, wxDialog )

BEGIN_EVENT_TABLE( AboutBox, wxDialog )
END_EVENT_TABLE()

//Constructor
AboutBox::AboutBox(void)
{
    Init();
}

//Constructor
AboutBox::AboutBox( wxWindow* parent, wxWindowID id,
    const wxString& title,
    const wxPoint& pos,
    const wxSize& size,
    long style,
    const wxString& name )
{
    Init();
    Create( parent, id, title, pos, size, style, name );
}

//Destructor
AboutBox::~AboutBox(void)
{
}

//Create
bool AboutBox::Create( wxWindow* parent, wxWindowID id,
    const wxString& title,
    const wxPoint& pos,
    const wxSize& size,
    long style,
    const wxString& name )
{
    SetExtraStyle( wxWS_EX_BLOCK_EVENTS );
    wxDialog::Create( parent, id, title, pos, size, style, name );
    CreateControls();
    if ( GetSizer() )
    {
        GetSizer()->SetSizeHints( this );
    }
    Centre();
    return true;
}

//Init
void AboutBox::Init()
{
    mContentPanel   = NULL;
    mHeaderBitmap   = NULL;
    mAppNameText    = NULL;
    mCopyrightText  = NULL;
    mVersionText    = NULL;
    mBuildInfoText  = NULL;
}

//CreateControls
void AboutBox::CreateControls()
{
    AboutBox* aboutDlg = this;

    // Create the sizer
    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer( wxVERTICAL );
    aboutDlg->SetSizer( itemBoxSizer2 );

    // Create the content panel
    mContentPanel = new wxPanel( aboutDlg, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNO_BORDER | wxTAB_TRAVERSAL );
    mContentPanel->SetBackgroundColour( wxColor( 255, 255, 255 ) );
    itemBoxSizer2->Add( mContentPanel, 0, wxGROW, 0 );

    wxBoxSizer* itemBoxSizer4 = new wxBoxSizer( wxVERTICAL );
    mContentPanel->SetSizer( itemBoxSizer4 );

    mHeaderBitmap = new wxStaticBitmap( mContentPanel, wxID_ANY, wxNullBitmap, wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer4->Add( mHeaderBitmap, 0, wxGROW, 0 );

    wxGridBagSizer* itemGridBagSizer6 = new wxGridBagSizer( 0, 0 );
    itemGridBagSizer6->AddGrowableRow( 2 );
    itemGridBagSizer6->AddGrowableRow( 3 );
    itemGridBagSizer6->SetEmptyCellSize( wxSize( 10, 20 ) );
    itemBoxSizer4->Add( itemGridBagSizer6, 0, wxGROW | wxLEFT | wxRIGHT | wxBOTTOM, 10 );

    mAppNameText = new wxStaticText( mContentPanel, wxID_ANY, wxT( "" ), wxDefaultPosition, wxDefaultSize, 0 );
    mAppNameText->SetForegroundColour( wxColor( 255, 0, 0 ) );
    mAppNameText->SetFont( wxFont( 28, wxSWISS, wxNORMAL, wxNORMAL, false, wxT( "Arial Narrow" ) ) );
    itemGridBagSizer6->Add( mAppNameText, wxGBPosition( 0, 0 ), wxGBSpan( 1, 2 ), wxALIGN_LEFT | wxALIGN_BOTTOM | wxLEFT | wxRIGHT | wxTOP, 5 );

    wxStaticText* staticText8 = new wxStaticText( mContentPanel, wxID_ANY, wxT( "version" ), wxDefaultPosition, wxDefaultSize, 0 );
    staticText8->SetForegroundColour( wxColor( 192, 192, 192 ) );
    staticText8->SetFont( wxFont( 8, wxSWISS, wxNORMAL, wxBOLD, false, wxT( "Arial" ) ) );
    itemGridBagSizer6->Add( staticText8, wxGBPosition( 1, 0 ), wxGBSpan( 1, 1 ), wxALIGN_RIGHT | wxALIGN_TOP | wxLEFT | wxBOTTOM, 5 );

    mVersionText = new wxStaticText( mContentPanel, wxID_ANY, wxT( "" ), wxDefaultPosition, wxDefaultSize, 0 );
    mVersionText->SetForegroundColour( wxColor( 192, 192, 192 ) );
    mVersionText->SetFont( wxFont( 8, wxSWISS, wxNORMAL, wxBOLD, false, wxT( "Arial" ) ) );
    itemGridBagSizer6->Add( mVersionText, wxGBPosition( 1, 1 ), wxGBSpan( 1, 1 ), wxALIGN_LEFT | wxALIGN_TOP | wxLEFT | wxRIGHT | wxBOTTOM, 5 );

    mCopyrightText = new wxStaticText( mContentPanel, wxID_ANY, wxT( "" ), wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE );
    itemGridBagSizer6->Add( mCopyrightText, wxGBPosition( 2, 0 ), wxGBSpan( 1, 2 ), wxGROW | wxALL, 5 );

    mBuildInfoText = new wxStaticText( mContentPanel, wxID_ANY, wxT( "" ), wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE );
    itemGridBagSizer6->Add( mBuildInfoText, wxGBPosition( 3, 0 ), wxGBSpan( 1, 2 ), wxGROW | wxALL, 5 );

    itemBoxSizer4->Add( 5, 5, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5 );

    wxStaticLine* staticLine13 = new wxStaticLine( aboutDlg, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
    itemBoxSizer2->Add( staticLine13, 0, wxGROW, 0 );

    wxStdDialogButtonSizer* dlgButtonSizer14 = new wxStdDialogButtonSizer;
    itemBoxSizer2->Add( dlgButtonSizer14, 0, wxALIGN_RIGHT | wxALL, 5 );

    wxButton* itemButton15 = new wxButton( aboutDlg, wxID_OK, wxT( "&OK" ), wxDefaultPosition, wxDefaultSize, 0 );
    dlgButtonSizer14->AddButton( itemButton15 );
    dlgButtonSizer14->Realize();

    mBuildInfoText->SetLabel( AboutBox::GetBuildInfo( wxBUILDINFO_LONG ) );
}

//GetBuildInfo
wxString AboutBox::GetBuildInfo( wxBuildInfoFormat format )
{
    wxString verString( wxVERSION_STRING );
    if ( format == wxBUILDINFO_LONG )
    {
#if defined( __WXMSW__ )
        verString << wxT( "-Windows" );
#elif defined( __WXMAC__ )
        verString << wxT( "-Mac" );
#elif defined( __UNIX__ )
        verString << wxT( "-Linux" );
#endif

#if wxUSE_UNICODE
        verString << wxT( "-Unicode" );
#else
        verString << wxT( "-ANSI" );
#endif
    }

    verString << wxT( " build." );
    return verString;
}

//ApplyInfo
void AboutBox::ApplyInfo()
{
    wxASSERT( mHeaderBitmap->GetBitmap().IsOk(), _( "Header bitmap for About box is empty" ) );
    SetTitle( wxString::Format( wxT( "%s %s" ), _( "About" ), mAppName.GetData() ) );
    mAppNameText->SetLabel( mAppName );
    mVersionText->SetLabel( mVersion );
    mCopyrightText->SetLabel( mCopyright );

    wxString buildInfo;
    if ( mCustomBuildInfo.IsEmpty() )
    {
        buildInfo = AboutBox::GetBuildInfo( wxBUILDINFO_LONG );
    }
    else
    {
        buildInfo = mCustomBuildInfo;
    }

    mBuildInfoText->SetLabel( buildInfo );
    wxSize bmpSize = mHeaderBitmap->GetSize();
    int labelWidth = bmpSize.GetWidth() - 20;
    mVersionText->Wrap( labelWidth );
    mCopyrightText->Wrap( labelWidth );
    mBuildInfoText->Wrap( labelWidth );
    mContentPanel->Layout();
    mContentPanel->GetSizer()->Fit( mContentPanel );
    GetSizer()->Fit( this );
    Centre();
}

//GetAppName
wxString AboutBox::GetAppName() const                   { return mAppName; }

//SetAppName
void AboutBox::SetAppName( const wxString& name )       { mAppName = name; }

//GetVersion
wxString AboutBox::GetVersion() const                   { return mVersion; }

//SetVersion
void AboutBox::SetVersion( const wxString& version )    { mVersion = version; }

//GetCopyright
wxString AboutBox::GetCopyright() const                 { return mCopyright; }

//SetCopyright
void AboutBox::SetCopyright( const wxString& copy )     { mCopyright = copy; }

//GetCustomBuildInfo
wxString AboutBox::GetCustomBuildInfo() const           { return mCustomBuildInfo; }

//SetCustomBuildInfo
void AboutBox::SetCustomBuildInfo( const wxString& info )   { mCustomBuildInfo = info; }

//SetHeaderBitmap
void AboutBox::SetHeaderBitmap( const wxBitmap& value )
{
    mHeaderBitmap->SetBitmap( value );
}

//SetHeaderBitmap
void AboutBox::SetHeaderBitmap( const wxString& name )
{
    wxBitmap bitmap( name, wxBITMAP_TYPE_ANY );
    mHeaderBitmap->SetBitmap( bitmap );
}



//
////wxGetBitmapFromMemory
//wxBitmap wxGetBitmapFromMemory( const void* data, size_t length )
//{
//    wxMemoryInputStream stream( data, length );
//    return wxBitmap( stream, wxBITMAP_TYPE_ANY );
//}
